﻿using System;
using System.Text;
using System.Security.Cryptography;
using Database;
using Process;

namespace ObjectManager
{
    public delegate void Login(object sender, InfoLogin e);

    public class InfoLogin : EventArgs
    {
        private DangNhap _login = null;

        public DangNhap Login
        {
            get { return _login; }
            set { _login = value; }
        }
    }

    public class Security
    {
        public static string GetMd5(string input)
        {
            string str = "";
            byte[] buffer = Encoding.UTF8.GetBytes(input);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            buffer = md5.ComputeHash(buffer);
            foreach (byte item in buffer)
            {
                str += item.ToString("X2");
            }
            return str;
        }
    }

    class Connection
    {
        public static ConnectionDB Connect()
        {
            ConnectionDB cnn;
            FactoryDatabase factory = new CreateDB(TypeDB.MSSQL);
            Config ft = new Config("Config\\Config.ini");
            if (ft.ReadValue("DatabaseFile", "ok") == "1")
            {
                string connection = ft.ReadValue("DatabaseFile", "path");
                cnn = MSSqlConnection.Create(connection);
                return cnn;
            }
            else
            {
                string server = ft.ReadValue("Database", "server");
                string database = ft.ReadValue("Database", "database");
                if (!string.IsNullOrEmpty(ft.ReadValue("Database", "user")))
                {
                    string user = null;
                    string password = "";
                    user = ft.ReadValue("Database", "user");
                    if (ft.ReadValue("Database", "password") != null)
                    {
                        password = ft.ReadValue("Database", "password");
                    }
                    cnn = factory.Create(server, database, user, password);
                    return cnn;
                }
                cnn = factory.Create(server, database);
                return cnn;
            }
        }
    }
}
